package com.huike.web.controller.clues;



import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.impl.TbClueServiceImpl;
import com.huike.clues.service.impl.TbClueTrackRecordServiceImpl;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 线索跟进记录Controller
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/clues/record")
public class TbClueTrackRecordController extends BaseController {
    @Autowired
    private TbClueTrackRecordServiceImpl tbClueTrackRecordService;

    @Autowired
    private TbClueServiceImpl tbClueService;
    /**
     * 查询线索跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("clueId")Long clueId) {
        List<TbClueTrackRecord> list = tbClueTrackRecordService.selectClueTrackRecord(clueId);
        return getDataTable(list);
    }


    /**
     * 新增线索跟进记录
     */
    @PreAuthorize("@ss.hasPermi('clues:record:add')")
    @Log(title = "线索跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody ClueTrackRecordVo tbClueTrackRecord) {

        //从VO类做转换类型
        TbClueTrackRecord tbClueTrack = new TbClueTrackRecord();
        BeanUtils.copyProperties(tbClueTrackRecord,tbClueTrack);
        //设置当前时间 --操作时间
        tbClueTrack.setCreateTime(DateUtils.getNowDate());
        //设置跟进人create_by
        tbClueTrack.setCreateBy(SecurityUtils.getUsername());
        //构建线索对象
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(tbClueTrackRecord,tbClue);
        //设置状态为进行中
        tbClue.setStatus("2");
        //设置id
        tbClue.setId(tbClueTrackRecord.getClueId());

        //调用tbClueService中update方法同步修改线索
        tbClueService.updateTbClue(tbClue);

        Integer num = tbClueTrackRecordService.add(tbClueTrack);
        if (num!=null){
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }
}
